//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "vSAjoutAdresse.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CComboBx"
#pragma link "CEdit"
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
//                      CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TAjoutAdresse::TAjoutAdresse(const int &nLogiciel,
                                        const int &nVersion,
                                        const AnsiString &asLogiciel,
                                        const AnsiString &asVersion,
                                        const AnsiString &asRef,
                                        TComponent* Owner)
    : TForm(Owner)
{
    CComboAttente * Attente = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        dmWebHelp = new TdmWebHelp((TComponent*)0);

        //Remplissage de la combo attente
        //-------------------------------

        Attente = new CComboAttente();
        Attente->Remplir(cbAttente->Items);


        cbRef->ItemIndex = 0;
        mPageWeb->Clear();

        m_nLogiciel = nLogiciel;
        m_nVersion  = nVersion;


        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"select ref_nom "+     //0
                                            "from refer "+
                                            "where log_code = "+m_nLogiciel+" and ver_code = "+m_nVersion;
        MaRequete->Open();
        MaRequete->First();
        eLogiciel->Text = asLogiciel;
        eVersion->Text  = asVersion;

        cbRef->Items->Add("<Nouvelle référence>");
        while(!MaRequete->Eof) {
            if(!VerifierRef(asRequete(0)) )
                cbRef->Items->Add( asRequete(0) );
            MaRequete->Next();
        }

        for(int i=0;i<cbRef->Items->Count; i++) {
            if(cbRef->Items->Strings[i] == asRef){
                cbRef->ItemIndex = i;
                GetPageDAttente(asRef);
            }
        }

        MaRequete->Sql->Clear();
        MaRequete->Close();


    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(Attente) {delete Attente; Attente = NULL;}
}
//---------------------------------------------------------------------------
__fastcall TAjoutAdresse::~TAjoutAdresse()
{
    try {
        if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//              EVENEMENT COMBO BOX REFERENCE
//---------------------------------------------------------------------------
void __fastcall TAjoutAdresse::cbRefChange(TObject *Sender)
{
    try {
        int idx = cbRef->ItemIndex;
        if( (idx != 0)&&(idx != -1) ) {
            cbRef->Style = csDropDownList;
            //Afficher la page d'attente
            GetPageDAttente(cbRef->Items->Strings[idx]);
        }else {
            cbRef->Style = csDropDown;
            cbAttente->ItemIndex = AUCUNE_SELECTION;
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
    }
}
//---------------------------------------------------------------------------
//      Obtenir la page d'attente associé a la reference
//---------------------------------------------------------------------------
void __fastcall TAjoutAdresse::GetPageDAttente(const AnsiString &asRef)
{
    TZZMySqlQuery * MaRequete = NULL;
    try {
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"select ref_attente "+     //0
                                            "from refer "+
                                            "where (log_code = "+m_nLogiciel+") and (ver_code = "+m_nVersion+")"+
                                            " and (ref_nom = "+QuotedStr(asRef)+")";
        MaRequete->Open();
        MaRequete->First();
        
        cbAttente->ItemIndex = nRequete(0);

        MaRequete->Sql->Clear();
        MaRequete->Close();
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        MaRequete = NULL;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
//---------------------------------------------------------------------------
//              CONSULTER LA PAGE WEB
//---------------------------------------------------------------------------
void __fastcall TAjoutAdresse::bbConsulterClick(TObject *Sender)
{
    CVerifInternet * VerifInternet = NULL;
    try {

        VerifInternet = new CVerifInternet();
        if(!Trim(mPageWeb->Text).IsEmpty()) {
            if(VerifInternet->ConnexionAuSite(mPageWeb->Text))
                ShellExecute(NULL, "Open", mPageWeb->Text.c_str(),NULL, NULL, SW_SHOWMAXIMIZED);
        }else
            MessageDlg("Veuillez remplir le champ Page Web", mtWarning, TMsgDlgButtons() << mbOK, 0);
        if(VerifInternet) {delete VerifInternet; VerifInternet = NULL;}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(VerifInternet) {delete VerifInternet; VerifInternet = NULL;}
}
//---------------------------------------------------------------------------
//              EVITER LES DOUBLONS DANS LA COMBO BOX
//---------------------------------------------------------------------------
bool __fastcall TAjoutAdresse::VerifierRef(const AnsiString &asRef)
{
    bool bRetour = false;
    try {
        int idx = cbRef->Items->Count;
        for( int i=0; i<idx; i++) {
            if(asRef == cbRef->Items->Strings[i]) {
                bRetour = true;
            }
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;}
    return bRetour;
}
//---------------------------------------------------------------------------
//              Transforme le premier caractere en majuscule
//---------------------------------------------------------------------------
AnsiString __fastcall TAjoutAdresse::MettreMajusculePrCar(const AnsiString &asText)
{
   AnsiString Text = "";
   try {
       for(int i=1;i<asText.Length()+1;i++) {
            if(i == 1)
                Text = UpperCase(asText[i]);
            else
                Text = Text + asText[i];
       }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        Text = "";}
    return Text;
}
//---------------------------------------------------------------------------
//                  Validation de l'ajout de l'adresse
//---------------------------------------------------------------------------
void __fastcall TAjoutAdresse::bbOkClick(TObject *Sender)
{
    try {
        int idx = cbRef->ItemIndex;
        if( (!Trim(cbRef->Text).IsEmpty()) && (idx != 0) && (!Trim(mPageWeb->Text).IsEmpty()) ) {
            if( cbRef->Text != "\\" && mPageWeb->Text != "\\") {
                if( VerifierRefAdresse() ) {
                    //Suppression de l'élément dont l'adresse est nulle
                    Supprimer(cbRef->Items->Strings[idx]);
                    ModalResult = mrOk;
                }else {
                    MessageDlg("Cette référence et cette adresse sont déjà existante.", mtWarning, TMsgDlgButtons() << mbOK, 0);
                    ModalResult = mrNone;
                }
            }else{
                MessageDlg("Veuillez mettre des caractères corrects.", mtWarning, TMsgDlgButtons() << mbOK, 0);
                ModalResult = mrNone;
            }
        }else {
            MessageDlg("Veuillez remplir les champs manquants.", mtWarning, TMsgDlgButtons() << mbOK, 0);
            ModalResult = mrNone;
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//              
//---------------------------------------------------------------------------
void __fastcall TAjoutAdresse::mPageWebChange(TObject *Sender)
{
    int idx = cbAttente->ItemIndex;
    cbAttente->ItemIndex = AUCUNE_SELECTION;
    if(Trim(mPageWeb->Text).IsEmpty())
        cbAttente->ItemIndex = idx;
}
//---------------------------------------------------------------------------
//              Supprimer le premier élément importé
//---------------------------------------------------------------------------
void __fastcall TAjoutAdresse::Supprimer(const AnsiString &asRef)
{
    TZZMySqlQuery * MaRequete = NULL;
    try {
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"delete from refer "+
                                            "where (log_code = "+m_nLogiciel+" and ver_code = "+m_nVersion+")"+
                                            " and ref_nom = "+QuotedStr(asRef)+" and ref_adresse is null";
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();

        MaRequete->Sql->Text = "Commit";
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();

        MaRequete->Close();
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        MaRequete = NULL;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
//---------------------------------------------------------------------------
//Vérifier si l'adresse et la ref sont déjà existante pour un logiciel et une version données
//---------------------------------------------------------------------------
bool __fastcall TAjoutAdresse::VerifierRefAdresse( void )
{
    bool bRetour = false;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"select count(ref_nom) "+
                                            "from refer "+
                                            "where (ref_nom = "+QuotedStr(cbRef->Items->Strings[cbRef->ItemIndex])+")"+
                                            " and (ref_adresse = "+QuotedStr(mPageWeb->Text)+")"+
                                            " and (ref_attente = "+cbAttente->ItemIndex+")"+
                                            " and (log_code ="+m_nLogiciel+")"+
                                            " and (ver_code = "+m_nVersion+")";
        MaRequete->Open();
        MaRequete->First();
        
        if( nRequete(0) == 0)
            bRetour = true;

        MaRequete->Close();
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        MaRequete = NULL;
        bRetour = false;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    return bRetour;
}
//---------------------------------------------------------------------------

